package com.ck.company.controller;

import com.ck.company.service.CpSceneService;
import com.ck.constant.ResponseConstant;
import com.ck.constant.SceneConstant;
import com.ck.model.common.sys.JsonResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 场景
 *
 * @author linqiang
 * @date 2018-06-20 18:33
 */
@Controller
@RequestMapping(value = "cpScene")
public class CpSceneController {
    private Logger logger = LoggerFactory.getLogger(CpSceneController.class);
    @Autowired
    private CpSceneService cpSceneService;

    /**
     * 保存场景
     *
     * @param sceneName 场景名称
     * @param request
     * @return Object
     */
    @RequestMapping(value = "saveScene")
    public Object saveScene(String sceneName, HttpServletRequest request) {
        if (StringUtils.isBlank(sceneName)) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, SceneConstant.NO_SCENE_NAME);
        }
        try {
            int row = cpSceneService.saveScene(sceneName, request);
            if (row == 1) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, SceneConstant.SAVE_SCENE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpSceneController saveScene 保存场景失败:" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, SceneConstant.SAVE_SCENE_FAIL);
    }


    /**
     * 删除场景
     *
     * @param sceneId 场景主键
     * @param request
     * @return Object
     */
    @RequestMapping(value = "deleteScene")
    public Object deleteScene(Long sceneId, HttpServletRequest request) {
        if (null == sceneId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, SceneConstant.NO_PARAM);
        }
        try {
            int row = cpSceneService.deleteScene(sceneId, request);
            if (row > 0) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, SceneConstant.DELETE_SCENE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpSceneController saveScene 删除场景失败:" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, SceneConstant.DELETE_SCENE_FAIL);
    }
}
